﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LordRewardManager : MonoBehaviour
{
    Text moneyValue;//元宝数
    //十只怪的名字
    string[] boosName = { "啸月牛魔", "噩梦之灵", "寒冰巨人", "幽冥魔狼", "无头骑士", "暗影之殇", "暗影恶魔", "炎魔苍熊", "炼狱男爵", "烟魂恐兽" };
    //品质
    string[] quality = { "普通", "优良", "史诗", "传说" };
    //相应品质的字体颜色
    Color[] color = { new Color(0, 255, 0, 1), new Color(0, 140, 255, 1), new Color(255, 0, 255), new Color(255, 255, 0, 1) };
    //4个显示的boos的序号
    int[] boosid = { 0, 0, 0, 0 };
    //四个怪的图片
    public Image[] boos;
    //四个框
    public GameObject[] kuang;
    //悬赏目标、悬赏奖励
    public Text[] t;
    //所有装备
    public Item[] item;
    //悬赏奖励的图片
    public Image[] images;
    //免费刷新次数、悬赏次数
    public Text[] t2;

    public GameObject[] yindao;


    private void Awake()
    {
        Random.InitState((int)System.DateTime.Now.Ticks);
    }
    void Start()
    {
        moneyValue = GameObject.Find("moneyValue").GetComponent<Text>();
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        t2[0].text = "免费刷新次数" + VariableDocument.GetInstance.renovate + "/2";
        t2[1].text = VariableDocument.GetInstance.reward_number + "/5";
        ChooseFour();
        if(VariableDocument.GetInstance.yindaoid == 21)
        {
            yindao[0].transform.GetChild(1).gameObject.SetActive(true);
            yindao[0].transform.GetChild(2).gameObject.SetActive(true);
        }
    }

    void Update()
    {
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        t2[0].text = "免费刷新次数" + VariableDocument.GetInstance.renovate + "/2";
        t2[1].text = VariableDocument.GetInstance.reward_number + "/5";
    }
    //十只怪随机四个
    void ChooseFour()
    {
        boosid[0] = Random.Range(0, 10);
        boosid[1] = Random.Range(0, 10);
        while(boosid[0] == boosid[1])
        {
            boosid[1] = Random.Range(0, 10);
        }
        boosid[2] = Random.Range(0, 10);
        while(boosid[0] == boosid[2] || boosid[1] == boosid[2])
        {
            boosid[2] = Random.Range(0, 10);
        }
        boosid[3] = Random.Range(0, 10);
        while (boosid[0] == boosid[3] || boosid[1] == boosid[3] || boosid[2] == boosid[3])
        {
            boosid[3] = Random.Range(0, 10);
        }
        //四张boos图片
        for(int a = 0; a < 4; a++)
        {
            //Debug.Log(boosid[a]);
            boos[a].sprite = Resources.Load<Sprite>("LordReward/boos/" + boosName[boosid[a]]);
            kuang[a].SetActive(false);
        }
        kuang[VariableDocument.GetInstance.current_quality].SetActive(true);
        
        RandomReward();
    }
    //奖励展示
    void RandomReward()
    {
        t[0].text = quality[VariableDocument.GetInstance.current_quality] + "." + boosName[boosid[VariableDocument.GetInstance.current_quality]];
        t[1].text = quality[VariableDocument.GetInstance.current_quality];
        t[0].color = color[VariableDocument.GetInstance.current_quality];
        t[1].color = color[VariableDocument.GetInstance.current_quality];
        VariableDocument.GetInstance.current_reward = Random.Range(0, 6);
        //Debug.Log(VariableDocument.GetInstance.current_reward);
        for(int b = 0; b < 6; b++)
        {
            images[b].sprite = item[VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, b]].itemImage;
        }
    }
    //点击免费刷新
    public void ClickRenovate()
    {
        if (VariableDocument.GetInstance.yindaoid == 21)
        {
            yindao[0].transform.GetChild(1).gameObject.SetActive(false);
            yindao[0].transform.GetChild(2).gameObject.SetActive(false);
            yindao[1].transform.GetChild(3).gameObject.SetActive(true);
            yindao[1].transform.GetChild(2).gameObject.SetActive(true);
        }
        if (VariableDocument.GetInstance.renovate > 0)
        {
            VariableDocument.GetInstance.renovate -= 1;
            ChooseFour();
        }
    }
    //点击元宝提品
    int c, d, e, f;
    int tipincishu = 0;
    public void ClickImproveQuality()
    {
        if (VariableDocument.GetInstance.yindaoid == 21 && tipincishu != 2)
        {
            tipincishu += 1;
        }
        if (VariableDocument.GetInstance.yindaoid == 21 && tipincishu == 2)
        {
            yindao[1].transform.GetChild(3).gameObject.SetActive(false);
            yindao[1].transform.GetChild(2).gameObject.SetActive(false);
            yindao[2].transform.GetChild(1).gameObject.SetActive(true);
            yindao[2].transform.GetChild(0).gameObject.SetActive(true);
        }
        if (VariableDocument.GetInstance.current_quality < 3 && VariableDocument.GetInstance.money>=120)
        {
            VariableDocument.GetInstance.money -= 120;
            c = 13;
            f = 4;
            StartCoroutine("DoSomething", 0.1f);
        }
        
        
    }
    IEnumerator DoSomething()
    {
        while (c > f)
        {
            d = (c - VariableDocument.GetInstance.current_quality) % 4;//当前
            if (d == 0)
            {
                d = 2;
            }
            else if (d == 2)
            {
                d = 0;
            }
            if (d == 0)
            {
                e = 3;
            }
            else
            {
                e = d - 1;
            }
            kuang[e].SetActive(false);
            kuang[d].SetActive(true);
            yield return new WaitForSeconds(0.1f);
            c--;
            if (c == f)
            {
                VariableDocument.GetInstance.current_quality = d;
                RandomReward();
            }
        }
    }

    //点击一键传说
    public void ClickHighestQuality()
    {
        if (VariableDocument.GetInstance.current_quality < 3 && VariableDocument.GetInstance.money >= 300)
        {
            VariableDocument.GetInstance.money -= 300;
            c = 17;
            f = 6+ VariableDocument.GetInstance.current_quality;
            StartCoroutine("DoSomething", 0.05f);
        }
    }
    //点击接取悬赏
    public void ClickTakeReward()
    {
        VariableDocument.GetInstance.yindaoid = 26;
        yindao[2].transform.GetChild(1).gameObject.SetActive(false);
        yindao[2].transform.GetChild(0).gameObject.SetActive(false);
        if (VariableDocument.GetInstance.reward_number > 0)
        {
            VariableDocument.GetInstance.reward_number -= 1;
            //随机怪
            VariableDocument.GetInstance.through = Random.Range(0, 7);
            GoToInterface.ToInterface("GameScene");
        }
    }
}
